﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using 安检系统.Model;
using 安检系统.Utils;

namespace 安检系统
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public static BaseInfo baseInfo;
        private static object _lock = new object();
        public static HttpServer httpServer = null;
        private static object _httpLock = new object();
        public static GlobalVariablesModel globalVariables = null;
        public static Config config = null;

        public static BaseInfo GetBaseInfo()
        {
            if (baseInfo == null)
            {
                lock (_lock)
                {
                    if (baseInfo == null)
                    {
                        baseInfo = new BaseInfo();
                        baseInfo.HttpServerIp = ConfigurationManager.AppSettings["HttpServerIp"];
                        baseInfo.HttpServerPort = ConfigurationManager.AppSettings["HttpServerPort"];
                        baseInfo.AuthHttpServerPort = ConfigurationManager.AppSettings["AuthHttpServerPort"];
                        baseInfo.LocalHttpServerPort = int.Parse(ConfigurationManager.AppSettings["LocalHttpServerPort"]);

                        baseInfo.RefreshSeconds = int.Parse(ConfigurationManager.AppSettings["RefreshSeconds"]);

                        baseInfo.IO_Com_PortName = ConfigurationManager.AppSettings["IO_Com_PortName"];
                        baseInfo.IO_In_Open_Port = ConfigurationManager.AppSettings["IO_In_Open_Port"];
                        baseInfo.IO_In_Close_Port = ConfigurationManager.AppSettings["IO_In_Close_Port"];

                        baseInfo.IO_Com_PortName2 = ConfigurationManager.AppSettings["IO_Com_PortName2"];
                        baseInfo.IO_In_Open_Port2 = ConfigurationManager.AppSettings["IO_In_Open_Port2"];
                        baseInfo.IO_In_Close_Port2 = ConfigurationManager.AppSettings["IO_In_Close_Port2"];

                        baseInfo.IO_Com_PortNameOut = ConfigurationManager.AppSettings["IO_Com_PortNameOut"];
                        baseInfo.IO_In_Open_PortOut = ConfigurationManager.AppSettings["IO_In_Open_PortOut"];
                        baseInfo.IO_In_Close_PortOut = ConfigurationManager.AppSettings["IO_In_Close_PortOut"];
                        baseInfo.BoudRate = ConfigurationManager.AppSettings["boudRate"];

                        baseInfo.RFID_sReaderIP = ConfigurationManager.AppSettings["RFID_sReaderIP"];
                        baseInfo.RFID_iReaderPoint = ConfigurationManager.AppSettings["RFID_iReaderPoint"];
                        baseInfo.RFID_sReaderIP2 = ConfigurationManager.AppSettings["RFID_sReaderIP2"];
                        baseInfo.RFID_iReaderPoint2 = ConfigurationManager.AppSettings["RFID_iReaderPoint2"];

                        baseInfo.Effluent_Standard = ConfigurationManager.AppSettings["Effluent_Standard"];

                        baseInfo.HikVideo_URL = ConfigurationManager.AppSettings["HikVideo_URL"];
                        baseInfo.HikVideo_Port = ConfigurationManager.AppSettings["HikVideo_Port"];
                        baseInfo.HikVideo_Username = ConfigurationManager.AppSettings["HikVideo_Username"];
                        baseInfo.HikVideo_Password = ConfigurationManager.AppSettings["HikVideo_Password"];
                        baseInfo.RfidRepeatedTime = ConfigurationManager.AppSettings["RfidRepeatedTime"];

                        baseInfo.TimeRange = ConfigurationManager.AppSettings["TimeRange"];
                        baseInfo.RFIDResetTime = ConfigurationManager.AppSettings["RFIDResetTime"];
                        baseInfo.RfidReader = ConfigurationManager.AppSettings["RfidReader"];
                        baseInfo.deduplicationTime = ConfigurationManager.AppSettings["deduplicationTime"];
                    }
                }
            }
            return baseInfo;
        }

        public static HttpServer GetHttpServer()
        {
            if (httpServer == null)
            {
                lock (_httpLock)
                {
                    if (httpServer == null)
                    {
                        int port = GetBaseInfo().LocalHttpServerPort;
                        httpServer = new HttpServer(port);
                    }
                }
            }
            return httpServer;
        }

        public static GlobalVariablesModel getGlobalVariables()
        {
            if (globalVariables == null)
            {
                lock (_lock)
                {
                    if (globalVariables == null)
                    {
                        globalVariables = new GlobalVariablesModel();
                    }
                }
            }
            return globalVariables;
        }

        public static Config getConfig()
        {
            if (config == null)
            {
                lock (_lock)
                {
                    if (config == null)
                    {
                        config = new Config();
                    }
                }
            }
            return config;
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            App.getConfig().materials = new List<Material>(0);
            String materials = ConfigurationManager.AppSettings["materials"];
            String[] materialArray = materials.Split(';');
            for (int i = 0; i < materialArray.Length; i++)
            {
                String item = materialArray[i];
                String[] itemArray = item.Split(',');
                Material material = new Material();
                material.Code = itemArray[0];
                material.Name = itemArray[1];
                App.getConfig().materials.Add(material);
            }
        }
    }
}